/**
 * Created by 小虎Oni on 2016/10/10.
 */
var AppDispatcher = require('../../utils/AppDisaptcher');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var md5 = require('md5');
var WebApiUtils = require('../../utils/WebApiUtils');
var config = require('../config');
var self = this;
var isDebug = config.isDebug;
function setData(pramsName,receivedTest){
    isDebug &&console.log("[LoginStore] serData:"+pramsName);
    self[pramsName] = receivedTest
}
function emitChange(type){
    isDebug &&console.log("[LoginStore] emitChange:"+type);
    LoginStore.emit(type);
}
var LoginStore = assign({},EventEmitter.prototype,{
    addChangeListener: function (type,callback) {
        this.on(type,callback)
    },
    removeChangeListener: function (type,callback) {
        this.removeListener(type,callback);
    },
    getData: function (pramsName) {
        isDebug &&console.log("[LoginStore] getData:"+pramsName);
        return self[pramsName]||null;
    }
});
function handleAction(action){
    console.log("-----------------LoginStore handleAction")
    if(action.type == "APP_LOGIN_ACTION"){
        loginFun(action.data)
    }
}
function loginFun(loginData){
    WebApiUtils.xHttpPost('/user/mobile/loginToken.json',{mobile:loginData.mobile}).then(function (data) {
        var loginToken = data.loginToken;
        var token = md5(md5(loginData.password)+loginToken);
        WebApiUtils.xHttpPost('/user/mobile/login.json',{mobile:loginData.mobile,password:token,rememberMe:false}).then(function (data) {
            alert("登录成功 用户id："+data.data.id);
        })
    })
}

LoginStore.dispatchToken = AppDispatcher.register(handleAction);
module.exports = LoginStore;